<template>
	<xn-form-container
		:title="formData.id ? '编辑打卡规则' : '增加打卡规则'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="24">
				<a-col :span="22">
					<a-form-item label="打卡规则：" name="ruleName">
						<a-input v-model:value="formData.ruleName" placeholder="请输入打卡规则" allow-clear />
					</a-form-item>
					<a-form-item label="打卡员工：" name="punchEmps">
						<a-tree-select
							v-model:value="formData.punchEmps"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择打卡员工"
							allow-clear
							multiple
							tree-default-expand-all
							:tree-data="treeData"
							:tree-default-expanded-keys="treeDefaultExpandedKeys"
							:field-names="{
								children: 'children',
								label: 'name',
								value: 'id'
							}"
						/>
					</a-form-item>
					<a-form-item label="白名单：" name="whiteList">
						<a-select
							v-model:value="formData.whiteList"
							placeholder="请选择白名单"
							allow-clear
							mode="multiple"
							:options="whiteListOptions"
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="24">
				<a-col :span="10">
					<a-form-item label="最早签到时间(上班之前/分钟)：" name="earlySignTime">
						<a-input-number v-model:value="formData.earlySignTime" placeholder="请输入最早签到时间" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="10">
					<a-form-item label="弹性时间(上班晚到多久不算迟到/分钟)：" name="elasticTime">
						<a-input-number v-model:value="formData.elasticTime" placeholder="请输入弹性时间" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="24">
				<a-col :span="10">
					<a-form-item label="晚到晚走：" name="enableNight">
						<a-switch :checked="enableNight" @change="editStatus" />
					</a-form-item>
				</a-col>
				<a-col :span="10" v-if="enableNight">
					<a-form-item label="晚到计时/分钟：" name="lateTime">
						<a-input-number v-model:value="formData.lateTime" placeholder="请输入晚到计时" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-form-item label="迟到分钟数：" name="minLate">
				小于&nbsp;<a-input-number
					style="width: 20%"
					v-model:value="formData.minLate"
					placeholder=""
					allow-clear
				/>分钟的迟到算作普通迟到。<br />
				<div style="height: 20px"></div>
				超过&nbsp;<a-input-number
					style="width: 20%"
					v-model:value="formData.minLate"
					placeholder=""
					allow-clear
					disabled
				/>分钟，小于<a-input-number
					style="width: 20%"
					v-model:value="formData.maxLate"
					placeholder=""
					allow-clear
				/>分钟的迟到算作严重迟到。<br />
				<div style="height: 20px"></div>
				超过&nbsp;<a-input-number
					style="width: 20%"
					v-model:value="formData.maxLate"
					placeholder=""
					allow-clear
					disabled
				/>分钟的迟到算作旷工迟到。
			</a-form-item>
			<a-form-item label="早退分钟数：" name="minEarly">
				小于&nbsp;<a-input-number
					style="width: 20%"
					v-model:value="formData.minEarly"
					placeholder=""
					allow-clear
				/>分钟的迟到算作普通早退。<br />
				<div style="height: 20px"></div>
				超过&nbsp;<a-input-number
					style="width: 20%"
					v-model:value="formData.minEarly"
					placeholder=""
					allow-clear
					disabled
				/>分钟，小于<a-input-number
					style="width: 20%"
					v-model:value="formData.maxEarly"
					placeholder=""
					allow-clear
				/>分钟的迟到算作严重早退。<br />
				<div style="height: 20px"></div>
				超过&nbsp;<a-input-number
					style="width: 20%"
					v-model:value="formData.maxEarly"
					placeholder=""
					allow-clear
					disabled
				/>分钟的迟到算作旷工早退。
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="sysPunchRuleForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysPunchRuleApi from '@/api/biz/sysPunchRuleApi'
	import userApi from '@/api/sys/userApi'
	import tool from '@/utils/tool'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const treeDefaultExpandedKeys = ref([])
	const treeData = ref([])
	const whiteListOptions = ref([])
	const enableNight = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			formData.value.whiteList = JSON.parse(formData.value.whiteList)
			formData.value.punchEmps = JSON.parse(formData.value.punchEmps)
			enableNight.value = formData.value.enableNight == 1 ? true : false
		}
		nextTick(() => {
			// 机构选择器数据
			userApi.userOrgTreeSelector().then((res) => {
				if (res !== null) {
					treeData.value = res
					// 默认展开2级
					treeData.value.forEach((item) => {
						// 因为0的顶级
						if (item.parentId === '0') {
							treeDefaultExpandedKeys.value.push(item.id)
							// 取到下级ID
							if (item.children) {
								item.children.forEach((items) => {
									treeDefaultExpandedKeys.value.push(items.id)
								})
							}
						}
					})
				}
			})
			// 用户下拉
			userApi.userSelectorList().then((res) => {
				whiteListOptions.value = res
			})
		})
	}
	const editStatus = (val) => {
		enableNight.value = val
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.whiteList = JSON.stringify(formDataParam.whiteList)
			formDataParam.punchEmps = JSON.stringify(formDataParam.punchEmps)
			enableNight.value == true ? 1 : 0
			formDataParam.enableNight = enableNight.value == true ? 1 : 0
			sysPunchRuleApi
				.sysPunchRuleSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
